
***************************************************************************************************************
/*
THIS DO-FILE:

1. Gets RAND data, merges on assisted living data
2. Generates lots of variables
3. Creates HRS_sample.dta (no dropped individuals)
4. Creates parent_sample.dta (only includes single retirees for analysis)
*/
***************************************************************************************************************

clear all
set more off
set maxvar 20000

***************************************************************************************************************


use "$rawdata\randhrs1992_2018v1_STATA\randhrs1992_2018v1.dta", clear
rename hhid HHID
destring HHID, replace

merge 1:1 hhidpn using "assistedliving_all.dta", keep(master match) nogen

forval w=4/12 {
	replace assliv`w'=. if inw`w'!=1
}

*INTERVIEW STATUS*
forval x=1/12 {
	rename r`x'iwstat iwstat`x'
}

gen female=(ragender==2) if inlist(ragender,1,2)

*AGE*
forval x=1/12 {
	rename r`x'agey_m age`x'
}

*MEDICAID*
forval x=1/12 {
	rename r`x'govmd medicaid`x'
}

*COHORTS*
gen 	cohort=1 if inlist(age4,65,66,67,68,69)
replace cohort=2 if inlist(age4,70,71,72,73,74)
replace cohort=3 if inlist(age4,75,76,77,78,79)
replace cohort=4 if inlist(age4,80,81,82,83,84)
replace cohort=5 if inlist(age4,85,86,87,88,89)
replace cohort=6 if inlist(age4,90,91,92,93,94)

*single/partnered
forval x=1/12 {
	gen single`x'=inlist(r`x'mstat,5,7,8) if !mi(r`x'mstat)
	gen partnered`x'=inlist(r`x'mstat,1,2,3) if !mi(r`x'mstat)
	gen widowed`x'=(r`x'mstat==7) if !mi(r`x'mstat)
	gen nevermarried`x'=(r`x'mstat==8) if !mi(r`x'mstat)
}

*census region
forval w=4/12 {
	rename r`w'cenreg cenreg`w'
}

*has kids?
forval x=1/12 {
	gen haskid`x'=(h`x'child>0) if !mi(h`x'child)
}

*number of kids
forval x=1/12 {
	gen numkids`x'=(h`x'child) if !mi(h`x'child)
}

*siblings
forval x=1/12 {
	gen nosib`x'=(r`x'livsib==0) if !mi(r`x'livsib)
}

*nursing home?
forval x=3/12 {
	rename r`x'nhmliv nh`x'
}

*INCOME*
*first, deflators:
*from http://www.ssa.gov/oact/STATS/avgcpi.html (CPI-W, what Lockwood/SSA uses to adjust SS benefits)
*income/earnings correspond to previous year
scalar cpi1=134.250 //1991
scalar cpi2=142.058 //1993
scalar cpi3=149.758 //1995
scalar cpi4=157.558 //1997
scalar cpi5=163.233 //1999
scalar cpi6=173.517 //2001
scalar cpi7=179.808 //2003
scalar cpi8=190.975 //2005
scalar cpi9=202.767 //2007
scalar cpi10=209.630 //2009
scalar cpi11=221.575 //2011
scalar cpi12=229.324 //2013
scalar cpi2010=213.967 //2010

*income=non-asset income
*income less transfers = non-asset income less cash value of means-tested govt transfers received
* (e.g. SSI). These are excluded because they arise endogenously in model.
forval x=1/12 {
	gen income`x'=h`x'itot
	replace income`x'=income`x'-h`x'icap
	replace income`x'=income`x'-r`x'issi if r`x'issi<.
	replace income`x'=income`x'-s`x'issi if s`x'issi<. 
	replace income`x'=income`x'-r`x'iunwc if r`x'iunwc<.
	replace income`x'=income`x'-s`x'iunwc if s`x'iunwc<.
	replace income`x'=income`x'-r`x'igxfr if r`x'igxfr<.
	replace income`x'=income`x'-s`x'igxfr if s`x'igxfr<.
	replace income`x'=income`x'*cpi2010/cpi`x'
	gen inc_ssret`x'=r`x'isret*cpi2010/cpi`x' //ok that doesn't include spouse because only using singles anyway
	gen inc_pen`x'=r`x'ipena*cpi2010/cpi`x'
}
egen avinc412=rowmean(income4 income5 income6 income7 income8 income9 income10 income11 income12)
*permanent income, permanent income quintile
xtile PIpctile=avinc412 [w=r4wtresp], nq(1000)
replace PIpctile=PIpctile/1000
xtile PIquintile=avinc412 [w=r4wtresp], nq(5)

forval x=1/12 {
	gen earn`x'=r`x'iearn*cpi2010/cpi`x'
}

*ASSETS*
forval x=2/12 {
	cap gen assets`x'=h`x'atotb*cpi2010/cpi`x'
	cap gen housingassets`x'=h`x'ahous*cpi2010/cpi`x'
}

*long-term care insurance, life insurance
forval x=1/12 {
	rename r`x'hiltc ltci`x'
	rename r`x'lifein lifei`x'
}


*health states
forval x=4/12 {
	rename r`x'adla adl`x'
	gen anyadl`x'=(adl`x'>0) if inlist(adl`x',0,1,2,3,4,5,9)
	rename r`x'iadla iadl`x'
	gen     cogimpaired`x'=(r`x'cogtot<=8) if r`x'cogtot<=35 //missing if proxy	
	rename r`x'shlt shlt`x'
}

save "HRS_sample.dta", replace

*********************************************************************
*SAMPLE SELECTION*
*********************************************************************
*only keep 1998-2014, waves 4-12
*at least 65 years old and has a kid in 1998
*single
*retirees (earnings <=$3000)
*no missing waves (unless dead)

count

keep if inw4==1 		  
count

keep if age4>=65 & age4!=.
count

keep if single4==1
keep if single5==1 | inw5==0				  
keep if single6==1 | inw6==0
keep if single7==1 | inw7==0
keep if single8==1 | inw8==0
keep if single9==1 | inw9==0				  
keep if single10==1 | inw10==0									
keep if single11==1 | inw11==0									
keep if single12==1 | inw12==0									
count
							
keep if haskid4==1
count

keep if earn4<=3000
keep if earn5<=3000 | inw5==0
keep if earn6<=3000 | inw6==0
keep if earn7<=3000 | inw7==0
keep if earn8<=3000 | inw8==0
keep if earn9<=3000 | inw9==0
keep if earn10<=3000 | inw10==0
keep if earn11<=3000 | inw11==0
keep if earn12<=3000 | inw12==0
count

keep if inlist(iwstat5,1,5,6)
keep if inlist(iwstat6,1,5,6)
keep if inlist(iwstat7,1,5,6)
keep if inlist(iwstat8,1,5,6)
keep if inlist(iwstat9,1,5,6)
keep if inlist(iwstat10,1,5,6)
keep if inlist(iwstat11,1,5,6)
keep if inlist(iwstat12,1,5,6)
count 

drop s1* s2* s3* s4* s5* s6* s7* s8* s9* s10*

save "parent_sample.dta", replace
***************************************************************************************************************